package agents;

import utils.Dijkstra;
import env.City;
import env.DistList;
import env.Move;
import env.Road;
import env.World;

public class TerroristBuster implements Agent {

	private World world;
	@SuppressWarnings("unused")
	private City goal;
	private boolean done;

	@SuppressWarnings({ "unused" })
	public TerroristBuster(World world2, City init, City goal) {
		this.world=world2;
		this.goal=goal;
		done = false;
	}


	@Override
	public Move calcMove() {
		City current = world.getCityOfAgent(this);
		DistList dist = Dijkstra.runDijkstra(world, current, true);
		City dest;
		City nearestTerrorist = dist.nearestTerrorist();
		if (nearestTerrorist == null) {//if no more terrorists or unreachable
			done=true;
			return null;
		}
		if (current.getEscorts() == 0) {
			dest = dist.nearestEscort();
			Road road = dist.getNextRoadInPath(dest);
			return dest != null ? new Move(this, road,//unreachable escort or no more escorts
					current, road.getNeighbor(current), false, false) : null;
		}
		if (current.getEscorts() > 0) {
			dest = nearestTerrorist;			
			Road road = dist.getNextRoadInPath(dest);
			if (road == null) road = current.getBlockedRoad();//if already in a city with blocked road
			return dest != null ? new Move(this, road,
					current, road.getNeighbor(current), true, false) : null;
		}
		return null;
	}


	@Override
	public boolean isDone() {
		return done;
		
	}


	@Override
	public City getGoal() {
		
		return goal;
	}


	@Override
	public void setCity(City to) {
		
	}
}
